package com.atguigu.gulimall.ware.advice;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author gaopeng
 * @date 2021/11/30
 **/
@Aspect
@Component
@Slf4j
public class LoggerAdvice {

    /**
     * 定义切点
     */
    @Pointcut(value="execution(* com.atguigu.gulimall.ware.controller.*.*(..))")
    public void myPointcut(){

    }

    @Around("myPointcut()")
    public Object myLogger(ProceedingJoinPoint pjp) throws Throwable{

        //获取类名
        String className=pjp.getTarget().getClass().toString();
        //获取方法名
        String methodName=pjp.getSignature().getName();
        //获取参数
        Object[] argsArray=pjp.getArgs();


        

        log.info("调用前："+className+":"+methodName+"传递的参数:"+ JSON.toJSONString(argsArray));

        //调用被增强的方法
        Object obj=pjp.proceed();

        log.info("调用后："+className+":"+methodName+"返回值:"+JSON.toJSONString(obj));

        return obj;
    }
}
